<template>
  <view class="page">
    <!-- 顶部余额卡片 -->
    <view class="wallet-card" v-if="type == 1">
      <view class="wallet-top">
        <text class="wallet-label">可用{{type_name}}(元):</text>
        <text class="wallet-amount">{{ balance }}</text>
      </view>
      <button class="btn-charge" @tap="onCharge">充值</button>
    </view>
	<!-- 顶部余额卡片 -->
	<view class="wallet-card" v-if="type == 2">
	  <view class="wallet-top">
	    <text class="wallet-label">可用{{type_name}}(元):</text>
	    <text class="wallet-amount">{{ member_profile.yajin }}</text>
	  </view>
	  <button class="btn-charge" @tap="back_yajin()" v-if="member_profile.yajin > 0">退回押金</button>
	</view>
    <!-- 明细列表 -->
    <view class="panel" v-if="records.length > 0">
      <view class="panel-hd">
        <text class="panel-title">余额明细</text>
        <view class="panel-month">
          <text class="month-arrow" @tap="prevMonth">‹</text>
          <text class="month-text">{{ currentYear }}年{{ currentMonth }}月</text>
          <text class="month-arrow" @tap="nextMonth">›</text>
        </view>
      </view>

      <view class="panel-bd">
        <view v-for="(item,idx) in records" :key="idx" class="record">
          <view class="record-left">
            <text class="record-type">{{ item.type }}</text>
            <text class="record-time">{{ item.time }}</text>
          </view>
          <text class="record-amount">{{ item.amount }}</text>
        </view>
      </view>
    </view>
	<view  class="empty-wrap" v-else>
	  <image class="empty-img" mode="widthFix" src="https://kehu-1256834579.cos.ap-shanghai.myqcloud.com/weier/empty.png"></image>
	  <text class="empty-text">暂无支付记录</text>
	</view>
  </view>
</template>

<script>
import common_config from '../../static/js/common.js';
var config = common_config.config;
var that;	
var userInfo;
export default {
  name: 'WalletPage',
  data() {
    return {
      balance: '0.00',
      currentYear: 2025,
      currentMonth: 8,
      records: [
       
      ],
	  type_name:'',
	  type:1,
	  member_profile:{},
	  can_back:0,
	  dzyjRowid:''
    }
  },
  onLoad(options) {
	that = this;
  	if(options.type == 1){
		that.type_name = '余额';
		uni.setNavigationBarTitle({
			title:'余额'
		});
	}
	if(options.type == 2){
		that.type_name = '押金';
		uni.setNavigationBarTitle({
			title:'押金'
		});
		that.plusReady();
	}
	that.type = options.type;
	//common_config.calert('test'+that.type);
  },
  methods: {
    onCharge() {
		
		//uni.showToast({ title: '去充值', icon: 'none' }); 
		uni.navigateTo({
			url:'/pages/index/recharge?type='+that.type
		});
	
	},
	//初始化
	plusReady(){
	  	userInfo = uni.getStorageSync('userInfo');
	  	if(userInfo){
	  		userInfo = JSON.parse(userInfo);
	  		that.if_login = true;
			console.log(userInfo);
			that.load_row_id();
			that.if_can_back();
	  	}else{
	  		that.if_login = false;
			uni.redirectTo({
				url:'/pages/index/login'
			});
	  	}
	  	
	},
	load_row_id(){
		uni.request({
		  url: config.request_url+'/v2/open/worksheet/getFilterRows',
		  method: 'POST',
		  header: {
			'Content-Type': 'application/json'
		  },
		  data: {
			"appKey": config.appKey,
			"sign": config.sign,
			"worksheetId": "yhgl",
			"pageSize": 50,
			"pageIndex": 1,
			"filters": [
				{
					"controlId": "openid",
					"dataType": 2,
					"spliceType": 1,
					"filterType": 2,
					"value": userInfo.openid //传openid
				}
			]
		  },
		  success: (res) => {
			console.log('返回结果:', res.data);
			res = res.data;
			console.log(res);
			if(res.success){
				var datas = res.data.rows;
				for(var i = 0;i < datas.length;i++){
					if(datas[i].openid == userInfo.openid){
						
						var member_profile = {
							openid:datas[i].openid,
							nickName:datas[i].nicheng,
							photo:'',
							rowid:datas[i].rowid,
							mobile:datas[i].shoujihao,
							xinyongfen:datas[i].xinyongfen,
							yajin:datas[i].yajin,
							sfsx:datas[i].sfsx,
							dzyjtkzt:datas[i].dzyjtkzt,
							dzyjjl:datas[i].dzyjjl
						};
						
						that.member_profile = member_profile;
				
					}
				}
			}else{
				 uni.showToast({ title: res.data.msg || '数据获取失败', icon: 'none' });
			}
		  },
		  fail: (err) => {
			console.error('请求失败:', err);
			uni.showToast({ title: '网络错误', icon: 'none' });
		  }
		})  
	},
	if_can_back(){
		uni.request({
		  url: config.request_url+'/workflow/hooks/NjhkYmJkYTk1MWRhODQ3YTNmN2EzOWI2',
		  method: 'POST',
		  header: {
			'Content-Type': 'application/json'
		  },
		  data: {
			"appKey": config.appKey,
			"sign": config.sign,
			 "UserId":userInfo.rowid  // 用户rowid
		  },
		  success: (res) => {
			console.log('返回结果:', res.data);
			res = res.data;
			res = res.data;
			console.log(res);
			if(res.success){
				that.dzyjRowid = res.data.dzyjRowid;
				that.can_back = 1;
				//common_config.calert(res.errorMsg);
			}else{
				 uni.showToast({ title: res.data.msg || '数据获取失败', icon: 'none' });
			}
		  },
		  fail: (err) => {
			console.error('请求失败:', err);
			uni.showToast({ title: '网络错误', icon: 'none' });
		  }
		})  
	},
	back_yajin(){
		if(that.member_profile.yajin/1 == 0){
			
			return false;
		}
		if(!that.can_back){
			common_config.calert('您当前有未完成订单，暂时无法退回押金');
			return false;
		}
		uni.showModal({
			title:'是否退回押金',
			success(res) {
				if(res.confirm){
					uni.request({
					  url: config.request_url+'/workflow/hooks/NjhkYTZhNTc1MWRhODQ3YTNmNDA1NTMw',
					  method: 'POST',
					  header: {
						'Content-Type': 'application/json'
					  },
					  data: {
						"appKey": config.appKey,
						"sign": config.sign,
						"UserId":userInfo.rowid,// 用户rowid
						"DzyjId":that.dzyjRowid// 短租押金rowid
					  },
					  success: (res) => {
						console.log('返回结果:', res.data);
						res = res.data;
						res = res.data;
						console.log(res);
						if(res.success){
							uni.showToast({
								title:'提交成功'
							});
							that.member_profile.yajin = '0.00';
							that.can_back = 0;
						}else{
							 uni.showToast({ title: res.data.msg || '数据获取失败', icon: 'none' });
						}
					  },
					  fail: (err) => {
						console.error('请求失败:', err);
						uni.showToast({ title: '网络错误', icon: 'none' });
					  }
					});  
				}
			}
		})
		
	},
    prevMonth() {
      let m = this.currentMonth - 1; let y = this.currentYear;
      if (m < 1) { m = 12; y -= 1; }
      this.currentMonth = m; this.currentYear = y;
    },
    nextMonth() {
      let m = this.currentMonth + 1; let y = this.currentYear;
      if (m > 12) { m = 1; y += 1; }
      this.currentMonth = m; this.currentYear = y;
    }
  }
}
</script>

<style scoped>
.page { padding: 24rpx; background-color: #f5f6f7; min-height: 100vh; }
.wallet-card { position: relative; padding: 40rpx 40rpx 120rpx 40rpx; border-radius: 28rpx; background-image: url('https://kehu-1256834579.cos.ap-shanghai.myqcloud.com/weier/qianbao_bg.png'); background-size: cover; background-position: center; color: #ffffff; overflow: hidden; }
.wallet-top { display: flex; flex-direction: column; }
.wallet-label { font-size: 26rpx; opacity: 0.95; }
.wallet-amount { font-size: 56rpx; font-weight: 800; line-height: 1.1; letter-spacing: 2rpx; margin-top: 8rpx; }
.btn-charge { position: absolute; left: 80rpx; right: 80rpx; bottom: 20rpx; height: 72rpx; line-height: 72rpx; background-color: #2f2f2f; color: #ffffff; border-radius: 46rpx; font-size: 36rpx; text-align: center; border: none; }
.panel { margin-top: 28rpx; background-color: #ffffff; border-radius: 28rpx; padding: 24rpx; }
.panel-hd { display: flex; align-items: center; }
.panel-title { font-size: 30rpx; font-weight: 700; color: #1f1f1f; }
.panel-month { margin-left: auto; display: flex; align-items: center; }
.month-arrow { font-size: 36rpx; color: #6b6b6b; padding: 0 12rpx; }
.month-text { font-size: 26rpx; color: #6b6b6b; }
.panel-bd { margin-top: 8rpx; }
.record { display: flex; align-items: center; padding: 28rpx 0; border-bottom: 1rpx solid #f1f1f1; }
.record:last-child { border-bottom: none; }
.record-left { display: flex; flex-direction: column; }
.record-type { font-size: 28rpx; color: #3a3a3a; }
.record-time { font-size: 24rpx; color: #9a9a9a; margin-top: 8rpx; }
.record-amount { margin-left: auto; font-size: 30rpx; color: #666666; }
.empty-wrap { padding-top: 120rpx; display: flex; flex-direction: column; align-items: center; }
.empty-img { width: 460rpx; }
.empty-text { margin-top: 16rpx; font-size: 26rpx; color: #999999; }
</style>
